﻿using System;
using SoundServant;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SKYPE4COMLib;

namespace SoundServant.Controls
{
    public partial class ListenerControl : UserControl
    {
        public readonly Listener Listener;

        Brush readyBgBrush;
        Brush connectedBgBrush;
        Brush callingBgBrush;
        Brush disconnectedBgBrush;

        public ListenerControl(Listener _listener)
        {
            InitializeComponent();

            Listener = _listener;

            BrushConverter brushConverter = new BrushConverter();
            readyBgBrush = (Brush)brushConverter.ConvertFromString("#bdd2e4");
            connectedBgBrush = (Brush)brushConverter.ConvertFromString("#88c49c");
            disconnectedBgBrush = (Brush)brushConverter.ConvertFromString("#efefef");
            callingBgBrush = (Brush)brushConverter.ConvertFromString("#f2dc82");

            Draw();
            listener_CallStatusUpdated();

            Listener.Updated += new Listener.VoidEventHandler(listener_Updated);
            Listener.CallStatusUpdated += new Listener.VoidEventHandler(listener_CallStatusUpdated);
            Listener.CallStarted += new Listener.VoidEventHandler(Listener_CallStarted);
            Listener.CallFinished += new Listener.VoidEventHandler(Listener_CallFinished);
        }

        void Listener_CallFinished()
        {
            SS.OneSecondTimer.Tick -= OneSecondTimer_Tick;
        }

        void Listener_CallStarted()
        {
            SS.OneSecondTimer.Tick += new EventHandler(OneSecondTimer_Tick);
        }

        void OneSecondTimer_Tick(object sender, EventArgs e)
        {
            StatusLabel.Content = Sharing.ResolveCallStatus(Listener.CallStatus) + " " + SS.ParseTimeSpan(Listener.CallDuration);
        }

        void listener_CallStatusUpdated()
        {
            StatusLabel.Content = SoundServant.Sharing.ResolveCallStatus(Listener.CallStatus);

            if (Listener.State == ListenerState.InCall)
            {
                TelephoneImage.Source = new BitmapImage(new Uri("/SoundServant;component/Images/greenphone40x40.png", UriKind.Relative));
                StatusToggleLabel.Content = "Connected";
                StatusToggleInfoLabel.Content = "click here to disconnect from the meeting";
                ListenerBorder.Background = connectedBgBrush;
            }
            else if (Listener.State == ListenerState.Disconnected)
            {
                TelephoneImage.Source = new BitmapImage(new Uri("/SoundServant;component/Images/redphone40x40.png", UriKind.Relative));
                StatusToggleLabel.Content = "Not Connected";
                StatusToggleInfoLabel.Content = "click here to connect to the meeting";
                ListenerBorder.Background = disconnectedBgBrush;
            }
            else if (Listener.State == ListenerState.Ready)
            {
                TelephoneImage.Source = new BitmapImage(new Uri("/SoundServant;component/Images/bluephone40x40.png", UriKind.Relative));
                StatusToggleLabel.Content = "Ready to Connect";
                StatusToggleInfoLabel.Content = "click here to remove from the call";
                ListenerBorder.Background = readyBgBrush;
            }
            else if (Listener.State == ListenerState.Calling)
            {
                TelephoneImage.Source = new BitmapImage(new Uri("/SoundServant;component/Images/yellowphone40x40.png", UriKind.Relative));
                StatusToggleLabel.Content = "Connecting";
                StatusToggleInfoLabel.Content = "click here to cancel";
                ListenerBorder.Background = callingBgBrush;
            }
        }

        void listener_Updated()
        {
            Draw();
        }

        private void Draw()
        {
            NameLabel.Content = Listener.Name;
            ContactLabel.Content = Listener.Contact;
            IdLabel.Content = "[" + Listener.Id + "]";

            if (Listener.CallStatus != TCallStatus.clsUnknown)
            {
                if (StatusLabel.Visibility != Visibility.Visible) StatusLabel.Visibility = Visibility.Visible;
                StatusLabel.Content = SoundServant.Sharing.ResolveCallStatus(Listener.CallStatus);
            }
        }

        private void ListenerBorder_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Listener.Toggle();

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
